const Koa = require('koa');
const koaBody = require('koa-body');  //ctx.body解析器
const koaStatic = require('koa-static')
const error = require('koa-json-error');  //错误处理中间件
const parameter = require('koa-parameter')
const mongoose = require('mongoose')
const path = require('path');
const app = new Koa(); //实例化
const routing = require('./routes');
const { connectionStr } = require('./config')

mongoose.connect(connectionStr,() => console.log('MongoDB 连接成功了！'))
mongoose.connection.on('error',console.error)

app.use(koaStatic(path.join(__dirname,'public')))  //图片生产链接
app.use(error({
    postFormat:(e,{stack,...rest})=>process.env.NODE_ENV === 'production'?rest:{stack,...rest}
}));
app.use(koaBody({
    multipart:true,  //支持启用文件格式
    formidable:{
        uploadDir:path.join(__dirname,'./public/uploads'),
        keepExtensions:true,
    }
}));
app.use(parameter(app)); //校验请求体 全局的方法
routing(app);

app.listen(3000,()=> console.log('3000端口'));